#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* START:: code for stack */
#define STACK_SIZE 32

typedef struct stack {
	int data[STACK_SIZE];
	int pos;
} stack_t;

int push(stack_t *s, int new)
{
	if (s->pos >= STACK_SIZE)
		return -1;
	s->data[s->pos++] = new;
	return 0;
}

int pop(stack_t *s)
{
	if (s->pos > 0)
		return s->data[--s->pos];
	return 0;
}

int empty(stack_t *s)
{
	if (s->pos == 0)
		return 1;
	return 0;
}

void init_stack(stack_t *s)
{
	s->pos = 0;
	memset(s->data, 0, sizeof(int) * STACK_SIZE);
}
/* END:: code for stack */

/* START:: code for queue */
typedef struct queue {
	stack_t s1[1]; /* for in queue */
	stack_t s2[1]; /* for out queue */
} queue_t;

void init_queue(queue_t *q)
{
	init_stack(q->s1);
	init_stack(q->s2);
}

void in_queue(queue_t *q, int new)
{
	push(q->s1, new);
}

int out_queue(queue_t *q)
{
	if (!empty(q->s2))
		return pop(q->s2);
	while (!empty(q->s1)) {
		int tmp = pop(q->s1);
		push(q->s2, tmp);
	}
	return pop(q->s2);
}

int q_empty(queue_t *q)
{
	return empty(q->s1) && empty(q->s2);
}

/* END:: code for queue */


int main ()
{
	queue_t q[1];
	int i;

	init_queue(q);

	for(i = 0; i < 10; i++)
		in_queue(q, i);
	while (!q_empty(q)) {
		int tmp = out_queue(q);
		printf("%d ", tmp);
	}
	printf("\n");

	return 0;
}

